/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wicketsystem;

import java.util.Date;
import mylist.MyHashMap;

import mylist.MyArrayList;

/**
 *
 * @author Daryna_Ragimova
 */
public class WicketSystem {

    private MyHashMap<Integer, Card> cards = new MyHashMap<Integer, Card>();
    private int currentCardId = 0;
    private double currentTripCost = 0;
    private MyArrayList<Card> rejectedEntries = new MyArrayList<Card>();
    private MyArrayList<Card> permittedEntries = new MyArrayList<Card>();

    public WicketSystem(double currentCost) {
        this.currentTripCost = currentCost;
    }

    public void setCurrentTripCost(double currentTripCost) {
        this.currentTripCost = currentTripCost;
    }
    // I don't know is there a lot of wickets in the system(all of them
    // are saved in collection) or no. So I didn't make any variable for the wicket.
    // I think it is not a problem to add it
    public Wicket createWicket() {
        Wicket wicket = new Wicket(this);
        return wicket;
    }

    public Card createTermLimitedCard(Types type, int term) {
        if (term <= 0){
            return null;
        }
        Card card = new TypicalCard(currentCardId++, type, term, new Date());
        cards.put(currentCardId, card);
        return card;
    }

    public Card createAccumulativeCard(double money) {
        if (money < 0){
            return null;
        }
        Card card = new AccumulativeCard(currentCardId++,currentTripCost, money);
        cards.put(currentCardId, card);
        return card;
    }

    public Card createQuantityLimitedCard(int quantityOfTrips) {
        if (quantityOfTrips <= 0){
            return null;
        }
        Card card = new QuantityOfTripsCard(currentCardId++, quantityOfTrips);
        cards.put(currentCardId, card);
        return card;
    }

    public boolean check(Card card) {
        if (card == null || !cards.containsValue(card)) {
            return false;
        }
        if (card.check()) {
            card.getTrip();
            permittedEntries.add(card);
            return true;
        }
        rejectedEntries.add(card);
        return false;
    }

    public boolean addMoneyToCard(AccumulativeCard card, double sum) {
        if (card == null || sum < 0) {
            return false;
        }
        if (cards.containsValue(card)) {
            card.addMoney(sum);
            return true;
        }
        return false;
    }

    public MyArrayList<Card> getAllRejections() {
        return rejectedEntries;
    }

    public MyArrayList<Card> getAllPermissions() {
        return permittedEntries;
    }

    public MyArrayList<Card> getRejections(Types type) {
        return getStatistics(false, type);
    }

    public MyArrayList<Card> getPermissions(Types type) {
        return getStatistics(true, type);
    }

    private MyArrayList<Card> getStatistics(boolean flag, Types type) {
        MyArrayList<Card> array;
        if (flag){
            array = permittedEntries;
        }else{
            array = rejectedEntries;
        }
        MyArrayList<Card> result = new MyArrayList<Card>();
        Card card;
        for (int i = 0; i < array.size(); i++) {
            card = array.get(i);
            if (card.getType().equals(type)) {
                  result.add(card);
            }
        }
        return result;
    }
    public boolean cardIsRegistered(Card card){
        return cards.containsValue(card);
    }

}
